package com.xu.demo.feature.mora;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

/**
 * @ClassName: GameFrame
 * @Description: 游戏界面
 * @author 徐静静真帅啊
 * @date 2020-7-28
 */
public class GameFrame extends JFrame{
	
	//序列化一下不迷路
	private static final long serialVersionUID = 1L;
	//用户选择变量
	static int select = 0;
	//选择按钮
	static JButton button1;
	static JButton button2;
	static JButton button3;
	//展示结果
	static JButton button4;
	static JButton button5;
	static JLabel label4;
	
	//路径千万条 适合最重要
//	ImageIcon icon1=new ImageIcon("/1.png");
//	ImageIcon icon2=new ImageIcon("/2.png");
//  ImageIcon icon3=new ImageIcon("/3.png");
	static final ImageIcon icon1=new ImageIcon(GameFrame.class.getResource("1.png"));
	static final ImageIcon icon2=new ImageIcon(GameFrame.class.getResource("2.png"));
	static final ImageIcon icon3=new ImageIcon(GameFrame.class.getResource("3.png"));
	
	public GameFrame() {
		// 确保一个漂亮的外观风格
		setDefaultLookAndFeelDecorated(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// 创建及设置窗口
		setTitle("石头剪刀布");
		setBounds(400, 200, 680, 400);
		
		JPanel jPanel = new JPanel();
		jPanel.setLayout(null);
		add(jPanel);
		
		// 添加 "Hello World" 标签
		JLabel label = new JLabel("请点击猜拳按钮，开始猜拳：");
		label.setBounds(20,20,200,40);
		jPanel.add(label);
        
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();
        
        button1.setBounds(80, 60, 60, 60);
        jPanel.add(button1);
        
        button1.setIcon(icon1);
        
        button2.setBounds(180, 60, 60, 60);
        jPanel.add(button2);
        button2.setIcon(icon2);
        
        button3.setBounds(280, 60, 60, 60);
        jPanel.add(button3);
        button3.setIcon(icon3);
        
        JLabel label2 = new JLabel("你出的是：");
		label2.setBounds(20,120,200,40);
		jPanel.add(label2);
		
		button4 = new JButton();
		button4.setBounds(180, 130, 60, 60);
		button4.setVisible(false);
		jPanel.add(button4);
		
		JLabel label3 = new JLabel("电脑出的是：");
		label3.setBounds(20,180,200,40);
		jPanel.add(label3);
		
		button5 = new JButton();
		button5.setBounds(180, 200, 60, 60);
		button5.setVisible(false);
		jPanel.add(button5);
		
		
		label4 = new JLabel("恭喜，你赢了！");
		label4.setVisible(false);
		label4.setForeground(Color.red);
		label4.setBounds(180,300,200,40);
		jPanel.add(label4);
		
		
		
		button1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				select = 1;
				action();
			}
		});
		
		button2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				select = 2;
				action();
			}
		});
		
		button3.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				select = 3;
				action();
			}
		});
		
		setVisible(true);
	}
	

	void action() {
		//首先设置结果可见
		button4.setVisible(true);
		button5.setVisible(true);
		label4.setVisible(true);
		
		//玩家出拳图标
		ImageIcon userIcon;
		if(select == 1) {
			userIcon=icon1;
		}else if(select == 2) {
			userIcon=icon2;
		}else {
			userIcon=icon3;
		}
		button4.setIcon(userIcon);
		
		//电脑出拳图标
		int randomInt = randomInt();
		ImageIcon NPCIcon;
		if(randomInt == 1) {
			NPCIcon=icon1;
		}else if(randomInt == 2) {
			NPCIcon=icon2;
		}else {
			NPCIcon=icon3;
		}
		button5.setIcon(NPCIcon);
		
		//输赢台词
		String win = "恭喜你，你赢了！";
		String lose = "很可惜，你输了！";
		String peace = "平局！";
		
		//判断输赢
		//条件1：小的赢 除了s=1 r=3的情况 所以得s<r && (s!= 1 || r!= 3) 优化为 s-r!=-2
		//条件2：大的赢 只有s=3 r=1的情况 所以得 s=3 && r=1 优化为s-r==2
		if((select < randomInt && select - randomInt != -2) || select - randomInt == 2) {
			label4.setText(win);
		}else if(select == randomInt){
			label4.setText(peace);
		}else {
			label4.setText(lose);
		}
		//笨拙的方式
//		if(select == 1) {
//			if(randomInt == 1) {
//				label4.setText(peace);
//			}else if(randomInt == 2) {
//				label4.setText(win);
//			}else {
//				label4.setText(lose);
//			}
//		}else if(select == 2) {
//			if(randomInt == 1) {
//				label4.setText(lose);
//			}else if(randomInt == 2) {
//				label4.setText(peace);
//			}else {
//				label4.setText(win);
//			}
//		}else {
//			if(randomInt == 1) {
//				label4.setText(win);
//			}else if(randomInt == 2) {
//				label4.setText(lose);
//			}else {
//				label4.setText(peace);
//			}
//		}
	}
	
	/**
	 * @Title: randomInt
	 * @Description: 获取随机数
	 * @param @return 参数
	 * @return int 返回类型
	 * @throws
	 */
	int randomInt() {
		Random random = new Random();
		return random.nextInt(3)+1;
	}
}
